<?php

namespace app\api;

use app\common\controller\BaseComm;

//接口基类
class ApiBase extends BaseComm{

	public $is_login = 0;
	public $user_info = [];
	public $token_data = [];
	public $token = '';//

    public function __construct(){
        parent::__construct();

        $this->checkLogin();
    }
	
    //重载抽象方法
    public function checkLogin(){
    	$this->token = input('session_key');
    	$code = input('code');
    	if(!empty($this->token)){
			$token_data = \think\Loader::model('DeviceToken')->find($this->token);
	    	if(!empty($token_data)){
	    		$this->init_user_info($token_data);
	    	}else{
	    		$this->json_return(-90001, "Token登陆凭证已失效", ['session_key'=>$this->token,'is_login'=>0]);
	    	}
    	}elseif(empty($code) && empty($this->token)){//Token登陆凭证已失效
    		$this->json_return(-90001, "Token登陆凭证已失效", ['session_key'=>$this->token,'is_login'=>0]);
    	}
    }

    public function init_user_info($token_data){
		//缓存todo..
    	$this->user_info = db('user')->where("openid='{$token_data['openid']}'")->find();
        if(!empty($this->user_info))$this->user_info['level'] = get_user_level($this->user_info['exp']);
		$this->token_data = $token_data;//token信息
        $this->is_login = !empty($this->user_info) ? 1:0;//设置登陆状态

        return $this->user_info;
    }
   
}